package cn.example.zjgsu.service.Impl;

import cn.example.zjgsu.dao.*;
import cn.example.zjgsu.exception.MyException;
import cn.example.zjgsu.pojo.BriefPlanItem;
import cn.example.zjgsu.pojo.Plan;
import cn.example.zjgsu.pojo.Planitem;
import cn.example.zjgsu.pojo.Scenery;
import cn.example.zjgsu.pojo.result.ResultBriefPlan;
import cn.example.zjgsu.pojo.result.ResultMFR;
import cn.example.zjgsu.pojo.result.ResultScenery;
import cn.example.zjgsu.service.OneDayFoodPageServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author xuejiahui
 * @date 2020/3/26 -19:16
 */
@Service
public class OneDayFoodPageServerImpl implements OneDayFoodPageServer{

    @Autowired
    PlanMapper planMapper;
    @Autowired
    PlanitemMapper planitemMapper;
    @Autowired
    FoodRecordMapper foodRecordMapper;
    @Autowired
    SceneryMapper sceneryMapper;
    @Autowired
    CommentMapper commentMapper;

    @Transactional
    public int insertPlan(Plan plan) throws MyException {
        if(plan!=null){
            planMapper.insert(plan);
            int planid=plan.getId();
            List<Planitem> planitems = plan.getPlanitems();
            if(planitems!=null){
                for (Planitem planitem : planitems) {
                    System.out.println(planitem.toString());
                    planitem.setPlanid(planid);
                    planitemMapper.insert(planitem);
                }
                return 1;
            }else{
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                throw new MyException("请选择你想要吃的美食,并制定一个计划");
            }
        }else{
            throw new MyException("插入错误,请登录！！！");
        }
    }

    @Override
    public Plan getPlanByPlanId(int planid) {
        Plan plan = planMapper.selectPlanById(planid);
        if(plan ==null){
            throw new NullPointerException("请输入正确的planid");
        }
        return plan;
    }

    @Override
    public List<ResultBriefPlan> getPlanByUserId(String userid) throws MyException {
        List<Plan> plans = planMapper.selectPlanByAcount(userid);
        System.out.println(plans.size());
        List<ResultBriefPlan> resultBriefPlans =new ArrayList<>();
        if(plans.size()==0){
            throw new MyException("你还没有计划哦,赶快创建一个");
        }else{
            for (Plan plan : plans) {
                ResultBriefPlan resultBriefPlan = new ResultBriefPlan();
                resultBriefPlan.setId(plan.getId());
                List<BriefPlanItem> briefPlanItems = new ArrayList<>();
                for (Planitem planitem : plan.getPlanitems()) {
                    BriefPlanItem briefPlanItem = new BriefPlanItem();
                    briefPlanItem.setFoodname(planitem.getFoodname());
                    briefPlanItem.setFoodurl(planitem.getFoodurl());
                    briefPlanItem.setShopname(planitem.getShopname());
                    briefPlanItem.setKind(planitem.getKind());
                    briefPlanItems.add(briefPlanItem);
                }
                resultBriefPlan.setBriefPlanItems(briefPlanItems);
                resultBriefPlans.add(resultBriefPlan);
            }
            return  resultBriefPlans;
        }
    }

    @Override
    public List<ResultMFR> getRecommendFoodRecord(int cityid) throws MyException {
        List<ResultMFR> recommendFoodRecord= foodRecordMapper.getRecommendFoodRecordByCityID(cityid);
        if (recommendFoodRecord.size()==0){
            throw new MyException("抱歉请输入正确的城市id");
        }
        for (ResultMFR resultMFR : recommendFoodRecord) {
            String[] split = resultMFR.getPictures().split(";");
            resultMFR.setPictures(split[0]);
            resultMFR.setCommentnumber(commentMapper.getCommentNumberOfFD(resultMFR.getId()));
        }
        return  recommendFoodRecord;
    }

    @Override
    public int deletePlanById(int planid) {
        return planMapper.deleteByPrimaryKey(planid);
    }

    @Override
    public List<ResultScenery> getSceneryByShopId(int shopid) {
        List<Scenery> sceneries = sceneryMapper.selectByShopId(shopid);
        List<ResultScenery> resultSceneries= new ArrayList<>();
        for (Scenery scenery : sceneries) {
            ResultScenery resultScenery = new ResultScenery();
            resultScenery.setIntroduction(scenery.getIntroduction());
            resultScenery.setName(scenery.getName());
            resultScenery.setPictures(Arrays.asList(scenery.getPictures().split(";")));
            resultSceneries.add(resultScenery);
        }
        return resultSceneries;
    }

    @Override
    public int updatePlan(Plan plan) {
        List<Planitem> planitems = plan.getPlanitems();
        int a=0;
        for (Planitem planitem : planitems) {
            planitem.setPlanid(plan.getId());
            int id=planitemMapper.selectPrimaryKeyByKindAndPlanId(plan.getId(),planitem.getKind());
            planitem.setId(id);
            a=planitemMapper.updateByPrimaryKey(planitem);
        }
        return a;
    }

    @Override
    public ResultBriefPlan getBriefPlanById(int planid) {
        Plan plan = planMapper.selectPlanById(planid);
        ResultBriefPlan resultBriefPlan = new ResultBriefPlan();
        resultBriefPlan.setId(plan.getId());
        List<BriefPlanItem> briefPlanItems = new ArrayList<>();
        for (Planitem planitem : plan.getPlanitems()) {
            BriefPlanItem briefPlanItem = new BriefPlanItem();
            briefPlanItem.setFoodname(planitem.getFoodname());
            briefPlanItem.setFoodurl(planitem.getFoodurl());
            briefPlanItem.setShopname(planitem.getShopname());
            briefPlanItem.setKind(planitem.getKind());
            briefPlanItems.add(briefPlanItem);
        }
        resultBriefPlan.setBriefPlanItems(briefPlanItems);
        return resultBriefPlan;
    }

    @Override
    public List<ResultMFR> fuzzyQueryFoodRecord(String foodrecordtitle) throws MyException{
        List<ResultMFR> resultMFRS = foodRecordMapper.furryQueryFoodRecord(foodrecordtitle);
        if (resultMFRS.size()==0){
            throw new MyException("没有找到该食记");
        }
        for (ResultMFR resultMFR : resultMFRS) {
            String[] split = resultMFR.getPictures().split(";");
            resultMFR.setPictures(split[0]);
            resultMFR.setCommentnumber(commentMapper.getCommentNumberOfFD(resultMFR.getId()));
        }
        return  resultMFRS;
    }
}
